package com.yungu.swift.admin.route.finance;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.utils.ExcelUtil;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.system.finance.model.param.AdminOrderFlowPageParam;
import com.yungu.swift.system.finance.model.param.AdminOrderPayParam;
import com.yungu.swift.system.finance.model.vo.FinanceOrderVo;
import com.yungu.swift.system.finance.model.vo.OrderPayDetailVo;
import com.yungu.swift.system.finance.service.FinanceOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : cuixiuyin
 * @date : 2019/8/21
 */
@RestController
@RequestMapping("/admin/v1/finance/orderPay")
@Api(value = "【财务管理】", tags = "订单支付")
public class OrderPayAdmin {

    @Reference
    private FinanceOrderService financeOrderService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "订单支付信息分页", httpMethod = "POST")
    public ResponseData<PageVo<FinanceOrderVo>> queryPage(@RequestBody AdminOrderFlowPageParam orderFlowPageParam) {
        return financeOrderService.queryPageVo(orderFlowPageParam);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "订单支付信息导出", httpMethod = "POST")
    public void export(@RequestBody AdminOrderFlowPageParam orderFlowPageParam, HttpServletResponse response) {
        List<FinanceOrderVo> list = financeOrderService.queryPageVoExport(orderFlowPageParam).getData();
        Map<Object, Object>[] dictionaryMapArray = new Map[13];
        //支付方式字典
        dictionaryMapArray[5] = new HashMap<>(2);
        dictionaryMapArray[5].put(1, "乘客支付");
        dictionaryMapArray[5].put(2, "司机支付");
        //支付状态字典
        dictionaryMapArray[12] = new HashMap<>(4);
        dictionaryMapArray[12].put(0, "未支付");
        dictionaryMapArray[12].put(1, "已支付");
        dictionaryMapArray[12].put(2, "已支付（有退款）");
        dictionaryMapArray[12].put(3, "已支付（有退款）");
        ExcelUtil.export(response, "订单支付", list, 13,
                new String[]{"订单创建时间", "支付完成时间", "流水号", "第三方流水号", "订单号", "支付方式", "支付渠道",
                        "订单金额（元）", "额外费用（元）", "实付金额（元）", "优惠券抵扣（元）", "退款金额（元）", "支付状态"},
                new String[]{"createOn", "payTime", "financeNumber", "serialNumber", "orderNo", "paySide", "courseDescription",
                        "totalFare", "otherFare", "actualFare", "marketDeduceAmount", "refundAmount", "payStatus"},
                dictionaryMapArray);
    }

    @RequestMapping(value = "/countPayOrder", method = RequestMethod.POST)
    @ApiOperation(value = "查询订单已支付信息", httpMethod = "POST")
    public ResponseData<FinanceOrderVo> findPaidOrder(@RequestBody AdminOrderFlowPageParam orderFlowPageParam) {
        return financeOrderService.countPayOrder(orderFlowPageParam);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ApiOperation(value = "查询订单已支付信息", httpMethod = "POST")
    public ResponseData<PageVo<OrderPayDetailVo>> detail(@RequestBody AdminOrderPayParam adminOrderPayParam) {
        return financeOrderService.payDetail(adminOrderPayParam);
    }

    @RequestMapping(value = "/detailExport", method = RequestMethod.POST)
    @ApiOperation(value = "查询订单已支付信息", httpMethod = "POST")
    public void export(@RequestBody AdminOrderPayParam adminOrderPayParam,HttpServletResponse response) {
        ResponseData<List<OrderPayDetailVo>> responseData = financeOrderService.exportPayDetail(adminOrderPayParam);
        List<OrderPayDetailVo> list = responseData.getData();
        if(CollectionUtils.isEmpty(list)){
            list = new ArrayList<>();
        }
        Map<Object, Object>[] dictionaryMapArray = new Map[21];
        ExcelUtil.export(response, "订单支付信息", list, 21,
                new String[]{"订单号", "代理名称", "公司名称", "订单创建时间", "乘客名称", "乘客号码",
                        "司机名称", "司机号码", "乘客数量", "小件数量", "订单总金额", "实际支付金额",
                        "优惠金额", "司机支付金额", "微信支付金额", "支付宝支付金额", "余额支付金额", "平台抽成",
                        "代理抽成", "公司抽成", "司机抽成"},
                new String[]{"orderNo", "agentName", "companyName", "createOn", "passengerName", "passengerMobile",
                        "driverName", "driverMobile", "passengerNum", "parcelNum", "totalFare", "actualFare",
                        "couponFare", "driverPay", "wechatPay", "alipay", "balancePay", "platfomCommission",
                        "agentCommission", "companyCommission", "driverCommission"},
                dictionaryMapArray);
    }
}
